import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    config: {
      model: "",
      temperature: 0.7,
      messageCount: 0,
      url: "http://127.0.0.1:11434",
    },
    models: [],
  },
  getters: {
    config(state) {
      return state.config;
    },
    models(state) {
      return state.models;
    },
  },
  mutations: {
    setConfig: (state, { value, key }) => {
      state.config[key] = value;
    },
    setModels(state, data) {
      state.models = data;
    },
  },
  actions: {
    getModels({ commit, state }) {
      fetch(`${state.config.url}/api/tags`, {
        method: "GET",
        headers: {
          "Content-Type": "application/json",
        },
      })
        .then((res) => {
          return res.json();
        })
        .then((res) => {
          commit("setModels", res.models);
          commit("setConfig", { value: res.models[0].model, key: "model" });
        })
        .catch((err) => {
          console.error(err);
        });
    },
  },
});
